#!/bin/bash
human_memory(){
  while read B dummy; do
    [ $B -lt 1024 ] && echo ${B}B && break
    KB=$(echo "scale = 2; $B/1024" | bc | awk '{printf "%.2f", $0}')
    [ $(echo "$KB > 1024" | bc) -eq 0 ] && echo ${KB}K && break

    MB=$(echo "scale = 2; $KB/1024" | bc | awk '{printf "%.2f", $0}')
    [ $(echo "$MB > 1024" | bc) -eq 0 ] && echo ${MB}M && break

    GB=$(echo "scale = 2; $MB/1024" | bc | awk '{printf "%.2f", $0}')
    [ $(echo "$GB > 1024" | bc) -eq 0 ] && echo ${GB}G && break

    echo $(echo "scale = 2; $GB/1024" | bc | awk '{printf "%.2f", $0}')T && break
  done
}

arr=(10 20 50 100 200 1024 5120)
for(( i=0;i<${#arr[@]};i++)) do
  ./src/redis-cli flushall > /dev/null
  t="./src/redis-benchmark -h 127.0.0.1 -p 6379 -r 100000 -n 100000 -t set,get -P 16 -d ${arr[i]} -q"
  m1=$(./src/redis-cli info memory | sed -n '2p' | tr -cd "[0-9]")
  echo $t
  ${t}
  m2=$(./src/redis-cli info memory | sed -n '2p' | tr -cd "[0-9]")
  useMemory=$(echo "$m2-$m1" | bc)
  human=$(echo $useMemory | human_memory)
  dbSize=$(./src/redis-cli dbsize)
  echo -e "value大小：${arr[i]}B \nkey数量：${dbSize} \n内存消耗：${useMemory} ${human}"
  echo -e "平均一个kv占用内存：$(echo "$useMemory/$dbSize" | bc | human_memory)\n"
done;
./src/redis-cli flushall > /dev/null